package teach.shuji.java12.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import teach.shuji.java12.common.CommonResult;
import teach.shuji.java12.system.dto.departments.DepartmentAddParam;
import teach.shuji.java12.system.entity.Department;
import teach.shuji.java12.system.mapper.DepartmemtMapper;
import teach.shuji.java12.system.service.DepartmentService;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmemtMapper departmemtMapper;

    @Override
    public CommonResult<Long> insert(DepartmentAddParam param) {

        CommonResult<Long> result = new CommonResult<>();

//        //验证数据合法性
//        if(param.getDeptName().isBlank()){
//            result.setCode(500);
//            result.setMsg("请填写部门名");
//            return result;
//        }

        //同级存在同名部门，则不添加数据
        var query = new LambdaQueryWrapper<Department>();
        //select count(*) from sys_dpt where parentId=? and dptName=?
        query.eq(Department::getParentId, param.getParentId()).eq(Department::getDeptName, param.getDeptName());

       if(departmemtMapper.exists(query))
       {
           result.setCode(500);
           result.setMsg("子部门名已存在，请更换");
           return result;
       }

        //考虑使用mapstruct简化对象转换代码
        Department entity = new Department();  //java 11开始有var
        entity.setParentId(param.getParentId());
        entity.setDeptName(param.getDeptName());
        entity.setOrderNum(param.getOrderNum());
        entity.setLeader(param.getLeader());
        entity.setPhone(param.getPhone());
        entity.setEmail(param.getEmail());
        entity.setStatus(param.getStatus());

        departmemtMapper.insert(entity);

        result.setData(entity.getDeptId());

        return result;
    }

//    @Override
//    public void Update(Department dpt) {
//        departmemtMapper.updateById(dpt);
//    }
//
//    @Override
//    public int Delete(long id) {
//        return departmemtMapper.deleteById(id);
//    }
//
//    @Override
//    public List<Department> Select(DepartmentSelectParam selectParam) {
//        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
//        //  name like '%.....%' and status = ...
//        queryWrapper.like(selectParam.getName()!=null && !selectParam.getName().isBlank(), Department::getDeptName, selectParam.getName())
//                    .eq(selectParam.getStatus()!=null && !selectParam.getStatus().isBlank(),Department::getStatus, selectParam.getStatus());
//                 // .and(selectParam.getStatus()!='0', x -> x.eq(Department::getStatus, selectParam.getStatus()));
//
//        List<Department> result = departmemtMapper.selectList(queryWrapper);
//        return result;
//    }
//
//    @Override
//    public Department getById(long id) {
//        return departmemtMapper.selectById(id);
//    }
}
